from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from django.utils.translation import gettext_lazy

from bloguser.models import UserProfile

# 修改网页title和站点header。
admin.site.site_header = "博客管理后台"
admin.site.site_title = '管理后台'
admin.site.index_title = '我的博客后台'


@admin.register(UserProfile)
class AuthorAdmin(UserAdmin):
    list_display = ['username', 'author_name', 'email', 'is_active']
    '''设置可编辑字段'''
    list_editable = ('is_active',)
    fieldsets = (
        (None, {u'fields': ('username', 'password')}),
        (gettext_lazy('用户信息'),
         {'fields': ('author_name', 'email')}),
        (gettext_lazy('权限信息'),
         {'fields': ('is_active', 'is_staff', 'is_superuser',
                     'groups', 'user_permissions')}),
    )
    add_fieldsets = (
        (None, {u'fields': ('username', 'password1', 'password2')}),
        (gettext_lazy('用户信息'),
         {'fields': ('author_name', 'email')}),
        (gettext_lazy('权限信息'),
         {'fields': ('is_active', 'is_staff', 'is_superuser',
                     'groups', 'user_permissions')}),
    )

    # 增加Action行为具体方法
    def defunc(self, request, queryset):
        queryset.update(is_active='0')

    def addfunc(self, request, queryset):
        queryset.update(is_active='1')

    addfunc.short_description = "解除禁用"
    defunc.short_description = "禁用账户"
    actions = [defunc, addfunc]

    def save_model(self, request, obj, form, change):
        obj.user = request.user
        return super().save_model(request, obj, form, change)
